package com.changgou.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Goods;
import com.changgou.goods.pojo.Spu;
import com.changgou.service.SpuService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品spu信息控制层
 *
 * @author gaofeng
 * @date 2025/4/20 10:51
 */
@Api(value = "SpuController")
@RestController
@RequestMapping("/spu")
@CrossOrigin
public class SpuController {
    @Resource
    private SpuService spuService;

    /**
     * 商品审核
     *
     * @param spuId 商品的spuId
     * @return com.changgou.entity.Result
     */
    @PutMapping("/audit/{spuId}")
    public Result audit(@PathVariable(value = "spuId") Long spuId) {
        spuService.audit(spuId);
        return new Result<>(true, StatusCode.OK, "审核成功");
    }

    /**
     * 商品下架
     *
     * @param spuId 商品的spuId
     * @return com.changgou.entity.Result
     */
    @PutMapping("/offShelve/{spuId}")
    public Result offShelve(@PathVariable(value = "spuId") Long spuId) {
        spuService.offShelve(spuId);
        return new Result<>(true, StatusCode.OK, "审核成功");
    }

    /**
     * 商品上架
     *
     * @param spuId 商品的spuId
     * @return com.changgou.entity.Result
     */
    @PutMapping("/onShelve/{spuId}")
    public Result onShelve(@PathVariable(value = "spuId") Long spuId) {
        spuService.onShelve(spuId);
        return new Result<>(true, StatusCode.OK, "上架成功");
    }

    /**
     * 商品批量上架
     *
     * @param spuIds 商品的spuId集合
     * @return com.changgou.entity.Result
     */
    @PutMapping("/batchOnShelve")
    public Result batchOnShelve(@RequestBody List<Long> spuIds) {
        spuService.batchOnShelve(spuIds);
        return new Result<>(true, StatusCode.OK, "批量上架成功", spuService.batchOnShelve(spuIds));
    }

    /**
     * Spu分页条件搜索实现
     *
     * @param spu  查询条件
     * @param page 当前页
     * @param size 每页显示条数
     * @return spu信息分页结果
     */
    @ApiOperation(value = "Spu条件分页查询", notes = "分页条件查询Spu方法详情", tags = {"SpuController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Spu对象", value = "传入JSON数据") Spu spu, @PathVariable int page, @PathVariable int size) {
        PageInfo<Spu> pageInfo = spuService.findPage(spu, page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * Spu分页搜索实现
     *
     * @param page 当前页
     * @param size 每页显示多少条
     * @return spu信息分页结果
     */
    @ApiOperation(value = "Spu分页查询", notes = "分页查询Spu方法详情", tags = {"SpuController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        PageInfo<Spu> pageInfo = spuService.findPage(page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * 多条件搜索品牌数据
     *
     * @param spu 查询条件
     * @return spu数据列表
     */
    @ApiOperation(value = "Spu条件查询", notes = "条件查询Spu方法详情", tags = {"SpuController"})
    @PostMapping(value = "/search")
    public Result<List<Spu>> findList(@RequestBody(required = false) @ApiParam(name = "Spu对象", value = "传入JSON数据") Spu spu) {
        List<Spu> list = spuService.findList(spu);
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 根据ID删除品牌数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @ApiOperation(value = "Spu根据ID删除", notes = "根据ID删除Spu方法详情", tags = {"SpuController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Long")
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable Long id) {
        spuService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 修改Spu数据
     *
     * @param spu spu对象
     * @param id  主键
     * @return 修改结果
     */
    @ApiOperation(value = "Spu根据ID修改", notes = "根据ID修改Spu方法详情", tags = {"SpuController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Long")
    @PutMapping(value = "/update/{id}")
    public Result update(@RequestBody @ApiParam(name = "Spu对象", value = "传入JSON数据") Spu spu, @PathVariable Long id) {
        spu.setId(id);
        spuService.update(spu);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 新增Spu数据
     *
     * @param spu spu对象
     * @return 新增结果
     */
    @ApiOperation(value = "Spu添加", notes = "添加Spu方法详情", tags = {"SpuController"})
    @PostMapping(value = "/add")
    public Result add(@RequestBody @ApiParam(name = "Spu对象", value = "传入JSON数据", required = true) Spu spu) {
        spuService.add(spu);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * 根据ID查询Spu数据
     *
     * @param id 主键
     * @return Spu数据
     */
    @ApiOperation(value = "Spu根据ID查询", notes = "根据ID查询Spu方法详情", tags = {"SpuController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Long")
    @GetMapping("/findById/{id}")
    public Result<Spu> findById(@PathVariable Long id) {
        Spu spu = spuService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", spu);
    }

    /**
     * 查询Spu全部数据
     *
     * @return Spu数据列表
     */
    @ApiOperation(value = "查询所有Spu", notes = "查询所Spu有方法详情", tags = {"SpuController"})
    @GetMapping(value = "/findAll")
    public Result<List<Spu>> findAll() {
        System.out.println("准备休眠，测试并发数量");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("结束休眠，测试并发数量");
        return new Result<>(true, StatusCode.OK, "查询成功", spuService.findAll());
    }

    /**
     * 添加Goods
     *
     * @param goods 商品信息
     * @return com.changgou.entity.Result
     */
    @PostMapping("/save")
    public Result save(@RequestBody Goods goods) {
        spuService.saveGoods(goods);
        return new Result<>(true, StatusCode.OK, "保存成功");
    }

    /**
     * 根据spuId查询商品信息
     *
     * @param spuId 商品的spuId
     * @return com.changgou.entity.Result<com.changgou.goods.pojo.Goods>
     */
    @GetMapping("/goods/{spuId}")
    public Result<Goods> findGoodsById(@PathVariable(value = "spuId") Long spuId) {
        return new Result<>(true, StatusCode.OK, "查询成功", spuService.findGoodBySpuId(spuId));
    }
}
